//
//  CCGridView.swift
//  test02
//
//  Created by lzh on 2022/2/23.
//  Copyright © 2022 NO. All rights reserved.
//

import UIKit

class CCGridView: UIView {

    private var timer: DispatchSourceTimer?
    private var goSpace: Int = 0
    let queue = DispatchQueue(label: "myQueue", attributes: .concurrent)
    func addTimer() {
        timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global())

        timer?.schedule(deadline: .now() + .seconds(5), repeating: 0.04)
             // timer 添加事件
             timer?.setEventHandler {[weak self] in
                 print("timer事件")
                guard let weakSelf = self else {return}
                weakSelf.goSpace = weakSelf.goSpace%20
                weakSelf.goSpace += 1
                DispatchQueue.main.async {
                    weakSelf.layer.setNeedsDisplay()
                }
               
             }
        timer?.resume()
    }
    
    // timer暂停
        func stopTimer() {
            timer?.suspend()
        }
        // timer结束
        func cancleTimer() {
            guard let t = timer else {
                return
            }
            t.cancel()
            timer = nil
        }
    override var frame: CGRect {
        didSet {
            // frame改变需要重绘
            super.frame = frame

//            layer.setNeedsDisplay()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        addTimer()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func display(_ layer: CALayer) {
//        super.display(layer)
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
//                queue.async {[weak self] in
//                    guard let weakSelf = self else {return}
                  ///获取当前上下文
                    let ctx = UIGraphicsGetCurrentContext()
                    ctx?.setFillColor(UIColor.black.withAlphaComponent(0.45).cgColor)
                    ctx?.setLineWidth(0.8)
                    UIColor.lightGray.setStroke()
                    ctx?.addPath(subLinePath())
                    ctx?.drawPath(using: .fillStroke)
        
                    DispatchQueue.main.async {[weak self] in
                        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
                        UIGraphicsEndImageContext()
                        self?.layer.contents = image.cgImage
                    }
//                }
    }
    
//    func displayLayer(_ layer: CALayer) {
//        print("000000")
//    }

//    }
    
   
    
    func subLinePath() -> CGMutablePath {
        let path = CGMutablePath()
        let count: Int = Int(UIScreen.main.bounds.height/20)
        for i in 0..<count {
            let p_start = CGPoint(x: 0, y: 20*i+goSpace)
            let p_end = CGPoint(x: UIScreen.main.bounds.width, y: CGFloat(20*i+goSpace))
            let p_top =  CGPoint(x: 20*i, y: 0)
            let p_bottom = CGPoint(x: CGFloat(20*i), y: UIScreen.main.bounds.height)
            path.addLines(between: [p_start, p_end])
            path.addLines(between: [p_top, p_bottom])
        }
        return path
    }
}

class SView: UIView {
    
//    func displayLayer(layer: CALayer) {
//        print("0999")
//    }
}

